#!/bin/bash

# Ventura RMC Sled Power Cycle

rmc-hsc-power-cycle() {
    # LTC4287 MFR_REBOOT_CONTROL (FDh) Read/Write
    # [3]:
    #       REBOOT 0 Write a 1 to reboot.
    # [2:0]
    #       RBT_DL 100 Configures Auto-Reboot turn-on Delay (tDL(RBT)) after the REBOOT bit is set to 1
    #       RBT_DL[2:0] DELAY
    #       000 0.580 s
    #       001 1.16 s
    #       010 2.32 s
    #       011 4.64 s
    #       100 9.28 s
    #       101 18.6 s
    #       110 37.1 s
    #       111 74.2 s
    ret1=$(i2cget -y -f 10 0x44)
    ret2=$(i2cget -y -f 10 0x14)

    if [[ "$ret1" =~ ^0x[0-9A-Fa-f]+$ ]]; then
        echo "RMC Main Source AC"
        i2cset -y -f 10 0x44 0xfd 0x00
        i2cset -y -f 10 0x44 0xfd 0x0b
    elif [[ "$ret2" =~ ^0x[0-9A-Fa-f]+$ ]]; then
        echo "RMC Second Source AC"
        i2ctransfer -f -y 10 w1@0x14 0xec
    fi

    return 0

}

# RMC Sled Power Cycle
if [ "$1" == 0 ]
then
    echo "Staring RMC Sled Power Cycle"
    rmc-hsc-power-cycle
else
    echo "Invalid RMC Cycle"
    exit 1
fi
